package com.juyu.miliao.common.security;

import static com.juyu.miliao.common.msg.CommonErrorMsgs.*;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.juyu.miliao.common.msg.ErrorMsg;
import com.juyu.miliao.common.security.exception.*;
import com.juyu.miliao.common.util.ServletTool;

/**
 * 返回json格式的错误信息
 * @author zc
 *
 */
public class JsonAuthFailureHandler implements AuthenticationFailureHandler{
	private final ObjectMapper objectMapper;
	
	public JsonAuthFailureHandler(ObjectMapper objectMapper){
		this.objectMapper = objectMapper;
	}
	
	@Override
	public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
		ErrorMsg errorMsg;
		if(exception instanceof UsernameNotFoundException){
			response.setStatus(ILLEGAL_PARAM.getStatus().value());
			errorMsg = new ErrorMsg(ILLEGAL_PARAM, "用户名不存在");
		} else if(exception instanceof IllegalUsernameFormatFormatException){
			response.setStatus(ILLEGAL_PARAM.getStatus().value());
			errorMsg = new ErrorMsg(ILLEGAL_PARAM, "用户名格式错误");
		} else if(exception instanceof IllegalPasswordFormatException){
			response.setStatus(ILLEGAL_PARAM.getStatus().value());
			errorMsg = new ErrorMsg(ILLEGAL_PARAM, "密码格式错误");
		} else if(exception instanceof BadCredentialsException){
			response.setStatus(ILLEGAL_PARAM.getStatus().value());
			errorMsg = new ErrorMsg(ILLEGAL_PARAM, "用户名或密码错误");
		}else if(exception instanceof IllegalVerificationCodeFormatException){
			response.setStatus(ILLEGAL_PARAM.getStatus().value());
			errorMsg = new ErrorMsg(ILLEGAL_PARAM, "验证码格式错误");
		}else if(exception instanceof WrongUserNameException){
			response.setStatus(ILLEGAL_PARAM.getStatus().value());
			errorMsg = new ErrorMsg(ILLEGAL_PARAM, "手机号已存在");
		}else if(exception instanceof ThirdPartyLoginException){
			response.setStatus(ILLEGAL_PARAM.getStatus().value());
			errorMsg = new ErrorMsg(ERROR_THIRDPARTY, exception.getMessage());
		}else if (exception instanceof WrongNickNameException) {
			response.setStatus(ILLEGAL_PARAM.getStatus().value());
			errorMsg = new ErrorMsg(ILLEGAL_PARAM, "用户昵称已存在");
		}else if (exception instanceof IllegalDeviceNoFormatException) {
			response.setStatus(ILLEGAL_PARAM.getStatus().value());
			errorMsg = new ErrorMsg(ILLEGAL_PARAM, "设备号不能为空");
		}
		else if (exception instanceof AccountDisabledException) {
			response.setStatus(ACCOUNT_DYSABLE.getStatus().value());
			errorMsg = new ErrorMsg(ACCOUNT_DYSABLE, "账号禁用,请联系客服");
		}else{
			response.setStatus(SERVER_ERROR.getStatus().value());
			errorMsg = SERVER_ERROR;
		}
		ServletTool.writeJSONStr(response, objectMapper.writeValueAsString(errorMsg));
	}
	
}
